package com.cetool.autocms.service.sys.impl;

import cn.hutool.core.bean.BeanUtil;
import com.cetool.autocms.common.PageRequest;
import com.cetool.autocms.enumeration.OperateEnum;
import com.cetool.autocms.mapper.sys.RoleMapper;
import com.cetool.autocms.pojo.dto.sys.RoleInfoDTO;
import com.cetool.autocms.pojo.param.sys.QueryRoleListParam;
import com.cetool.autocms.pojo.param.sys.SaveRoleParam;
import com.cetool.autocms.pojo.request.sys.QueryRoleListRequest;
import com.cetool.autocms.pojo.request.sys.SaveRoleRequest;
import com.cetool.autocms.service.sys.RoleService;
import com.cetool.autocms.util.BeansUtil;
import com.cetool.autocms.util.UserInfoContextUtil;
import com.github.pagehelper.PageSerializable;
import com.github.pagehelper.page.PageMethod;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 角色管理服务实现层
 *
 * @author 戴越
 * @date 2021/11/25
 */
@Service
@Slf4j
public class RoleServiceImpl implements RoleService {
    /**
     * 注入角色管理持久层
     */
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public PageSerializable<RoleInfoDTO> listPageRoleInfo(PageRequest<QueryRoleListRequest> request) {
        QueryRoleListParam param = new QueryRoleListParam();
        BeanUtil.copyProperties(request.getData(), param);
        PageMethod.startPage(request.getPageNum(), request.getPageSize());
        return BeansUtil.pageCopy(new PageSerializable<>(roleMapper.getRoleInfoList(param)), RoleInfoDTO.class);
    }

    @Override
    public OperateEnum saveRole(SaveRoleRequest request) {
        SaveRoleParam param = new SaveRoleParam();
        BeanUtil.copyProperties(request, param);
        param.setUserId(UserInfoContextUtil.getUserId());
        roleMapper.saveRole(param);
        return OperateEnum.ADD_SUCCESS;
    }

}
